package com.unionPay.net;



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

import com.unionPay.utils.GlobalInfo;


public class HttpTask extends Task<Integer> {
	private String url;
	private String content;

	public HttpTask(TaskListener listener, String url, String content) {
		super(listener);
		this.url = url;
		this.content = content;
		cancelConnect = false;
	}

	@Override
	public Integer get() {
		int responseCode = -2;
		InputStream inputStream = null;
		InputStreamReader inputReader = null;
		BufferedReader reader = null;

		URL url = null;
		try {
			url = new URL(this.url.trim());
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}

		HttpURLConnection connectiong = null;
		try {
			connectiong = (HttpURLConnection) url.openConnection();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		connectiong.setConnectTimeout(GlobalInfo.netTimeOut);
		connectiong.setRequestProperty("Authorization", "Basic");
		connectiong.setRequestProperty("User-Agent", "Mozilla/5.0");
		connectiong.setRequestProperty("Content-type",
				"text/xml; charset=UTF-8");
		connectiong.setDoInput(true);
		connectiong.setDoOutput(true);
		
		try {
			connectiong.setRequestMethod("POST");
		} catch (ProtocolException e) {
			e.printStackTrace();

		}

		try {
			connectiong.getOutputStream().write(
					this.content.trim().getBytes("UTF-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {

			responseCode = connectiong.getResponseCode();
		} catch (IOException e) {
			e.printStackTrace();

		}

		if (cancelConnect) {
			responseCode = -1;
		} else {
			if (responseCode >= 200 && responseCode <= 299) {
				try {
					inputStream = (InputStream) connectiong.getInputStream();
				} catch (IOException e) {
					e.printStackTrace();
				}
				inputReader = new InputStreamReader(inputStream);
				reader = new BufferedReader(inputReader);
				String inputLine = null;
				StringBuffer strBuffer = new StringBuffer();
				try {
					while ((inputLine = reader.readLine()) != null) {
						strBuffer.append(inputLine);
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
				GlobalInfo.netResult = strBuffer.toString();
			} else {
				GlobalInfo.netResult = null;
			}
		}
		if (inputReader != null) {
			try {
				inputReader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			inputReader = null;
		}
		if (reader != null) {
			try {
				reader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			reader = null;
		}
		if (inputStream != null) {
			try {
				inputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			inputStream = null;
		}
		if (connectiong != null) {
			connectiong.disconnect();
			connectiong = null;
		}

		return responseCode;
	}
}
